package com.xinghou.platform.dao.main.aspect;

import com.xinghou.platform.dao.main.common.DBFavorityLevelCached;
import com.xinghou.platform.dao.main.common.DBStoreLevelCached;
import com.xinghou.platform.model.main.FavoriteCard;
import com.xinghou.platform.model.main.FavoriteSubject;
import com.xinghou.platform.model.main.StoreBrowse;
import com.xinghou.platform.util.JsonUtil;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

/**
 * Created by qingshengzheng on 16/7/6.
 */
@Aspect
@Component
public class FavorityCacheAspect extends DBFavorityLevelCached {
    @Pointcut("execution(public * com.xinghou.platform.dao.main.FavoriteDAO.delFavoriteCard(..))")
    public void delFavoriteCard() {
    }

    @Pointcut("execution(public * com.xinghou.platform.dao.main.FavoriteDAO.delFavoriteSubject(..))")
    public void delFavoriteSubject() {
    }

    @Pointcut("execution(public * com.xinghou.platform.dao.main.FavoriteDAO.addFavoriteCard(..))")
    public void addFavoriteCard() {
    }

    @Pointcut("execution(public * com.xinghou.platform.dao.main.FavoriteDAO.addFavoriteSubject(..))")
    public void addFavoriteSubject() {
    }
    @Pointcut("execution(public * com.xinghou.platform.dao.main.FavoriteDAO.hasFavoritySubject(..))")
    public void hasFavoritySubject() {
    }
    @Pointcut("execution(public * com.xinghou.platform.dao.main.FavoriteDAO.hasFavorityCard(..))")
    public void hasFavorityCard() {
    }


    @Around("hasFavoritySubject()")
    public int hasFavoritySubject(ProceedingJoinPoint pjp) throws Throwable {
        String userid = ((Object[]) pjp.getArgs())[0].toString();
        String subjectid = ((Object[]) pjp.getArgs())[1].toString();
        String key = String.format(CACHE_FAVORITY_SUBJECT, userid, subjectid);
        if("0".equals(getRedisClient().get(key,CACHED_DB_INDEX)))
            return 0;
        else if("1".equals(getRedisClient().get(key,CACHED_DB_INDEX)))
            return 1;
        else {
            int n= (int)  pjp.proceed();
            getRedisClient().set(key, n+"", CACHED_DB_INDEX);
            return n;
        }
    }

    @Around("hasFavorityCard()")
    public int hasFavorityCard(ProceedingJoinPoint pjp) throws Throwable {
        String userid = ((Object[]) pjp.getArgs())[0].toString();
        String cardid = ((Object[]) pjp.getArgs())[1].toString();
        String key = String.format(CACHE_FAVORITY_CARD, userid, cardid);
        if("0".equals(getRedisClient().get(key,CACHED_DB_INDEX)))
            return 0;
        else if("1".equals(getRedisClient().get(key,CACHED_DB_INDEX)))
            return 1;
        else {
         int n= (int)  pjp.proceed();
            getRedisClient().set(key, n+"", CACHED_DB_INDEX);
            return n;
        }
    }
    @Around("addFavoriteSubject()")
    public void addFavoriteSubject(ProceedingJoinPoint pjp) throws Throwable {
        FavoriteSubject favoriteSubject = (FavoriteSubject)((Object[])pjp.getArgs())[0];
        pjp.proceed();
        String key = String.format(CACHE_FAVORITY_SUBJECT, favoriteSubject.getUid(), favoriteSubject.getSubjectid());
        getRedisClient().set(key, "1", CACHED_DB_INDEX);
    }
    @Around("delFavoriteSubject()")
    public void delFavoriteSubject(ProceedingJoinPoint pjp) throws Throwable {
        String userid = ((Object[]) pjp.getArgs())[0].toString();
        String cardid = ((Object[]) pjp.getArgs())[1].toString();
        pjp.proceed();
        String key = String.format(CACHE_FAVORITY_SUBJECT, userid, cardid);
        getRedisClient().set(key, "0", CACHED_DB_INDEX);
    }
    @Around("delFavoriteCard()")
    public void delFavoriteCard(ProceedingJoinPoint pjp) throws Throwable {
        String userid = ((Object[]) pjp.getArgs())[0].toString();
        String subjectid = ((Object[]) pjp.getArgs())[1].toString();
        pjp.proceed();
        String key = String.format(CACHE_FAVORITY_CARD, userid, subjectid);
        getRedisClient().set(key, "0", CACHED_DB_INDEX);
    }
    @Around("addFavoriteCard()")
    public void addFavoriteCard(ProceedingJoinPoint pjp) throws Throwable {
        FavoriteCard favoriteCard = (FavoriteCard)((Object[])pjp.getArgs())[0];
        pjp.proceed();
        String key = String.format(CACHE_FAVORITY_CARD, favoriteCard.getUid(), favoriteCard.getCardid());
        getRedisClient().set(key, "1", CACHED_DB_INDEX);
    }



}
